
var numSchede=5;
var idUtente;

$( document ).ready(function() {
	
	idUtente=$("#idUtente").val();
	numSchede=$("#numSchede option:selected").val();
	
	$("#numSchede" ).change(function() {
		numSchede=$("#numSchede option:selected").val();
		gestioneSchede(idUtente, numSchede);
	});
	
	gestioneSchede(idUtente, numSchede);
	
	gestioneUtente(idUtente);
});    

function gestioneSchede(idUtente, numSchede) {
	$.ajax({
	    type: 'GET',
	    dataType:'json',
	    url: "http://localhost:8080/WeiWat/getSchedeUtente.action?idUtente="+idUtente+"&numSchede="+numSchede,
	    data: {}, 
	    success: function(responseData) {
	    	if ( responseData != null ) {
	    		htmlmyTabLi = "";
	    		htmlmyTabContent = "";
		    	$.each(responseData.schedaFoodBean,function(index, value) {
	    		
		    		schedaDate = formatDateToView(value.schedaDate, "A");
		    		htmlmyTabLi += "<li>" +
											"<a href=\"#\" onclick=\"javascript:showDiv(this, '" + value.idScheda + "')\" data-toggle=\"tab\">" +
												"<span class=\"glyphicon glyphicon-pencil\">&nbsp; " + schedaDate + "</span>" +
											"</a>" +
										"</li>";	
		    		
		    		htmlmyTabContent += "<div class=\"tab-pane active\" id=\"" + value.idScheda + "\"  >" +
											"<p>Scheda del " + schedaDate + "</p>" +
											"<div id=\"1\"><h2>Colazione</h2><ul><li>Pane</li><li>Cioccolata</li></ul></div>"
										"</div>";
		    	});
		    	$("#myTabLi").html(htmlmyTabLi);
		    	$("#myTabContent").html(htmlmyTabContent);
	    	}
	    },
	    error: function(XMLHttpRequest, textStatus, errorThrown) {
	    }
	});
}

function gestioneUtente(idUtente) {
	$.ajax({
	    type: 'GET',
	    dataType:'json',
	    url: "http://localhost:8080/WeiWat/getDettaglioUtente.action?idUtente="+idUtente,
	    data: {}, 
	    success: function(responseData) {
	    	
	    	label = "Benvenuto " + responseData.utenteBean.cognome + " " + responseData.utenteBean.nome;
	    	$("#headerMenu").html(label);
	    },
	    error: function(XMLHttpRequest, textStatus, errorThrown) {
	    }
	});
}

function showDiv(el, id){
	$("#myTabContent").children().hide(); 
	$("#"+id).show();
	$("#myTabLi").children().removeClass("active");
	el.parentNode.className= 'active';
}

function formatDateToView(date, format){
	viewDate = "";
	if (format=="A") {
		/** Convert date from yyyy-MM-ddThh:mm:ss to dd/MM/yyyy **/
		viewDate = date.substring(8, 10) + "/" + date.substring(5, 7) + "/" + date.substring(0, 4);
	}
	return viewDate;
}